package com.matt.system.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.matt.system.domain.TSysmenu;
import com.matt.system.service.SystemMenuService;
import com.opensymphony.xwork2.ActionSupport;
@Controller
public class SysMenuAction extends ActionSupport {
	
	/**
	 * 系统菜单
	 */
	private static final long serialVersionUID = 1L;

	@Autowired
	private SystemMenuService systemMenuService;
	
	private List<TSysmenu> list;
	
	private List<Map<String, String>> treeData;
	
	private String operation;//保存还是修改
	
	private String smname;
	
	private String smimage;
	
	private String smurl;
	
	private String smflag;
	
	private String tsmId;
	
	private String smcode;
	
	private String smorder;
	
	public String getTsmId() {
		return tsmId;
	}

	public void setTsmId(String tsmId) {
		this.tsmId = tsmId;
	}

	public String getSmcode() {
		return smcode;
	}

	public void setSmcode(String smcode) {
		this.smcode = smcode;
	}

	public String getSmorder() {
		return smorder;
	}

	public void setSmorder(String smorder) {
		this.smorder = smorder;
	}

	public String getOperation() {
		return operation;
	}

	public void setOperation(String operation) {
		this.operation = operation;
	}

	public String getSmname() {
		return smname;
	}

	public void setSmname(String smname) {
		this.smname = smname;
	}

	public String getSmimage() {
		return smimage;
	}

	public void setSmimage(String smimage) {
		this.smimage = smimage;
	}

	public String getSmurl() {
		return smurl;
	}

	public void setSmurl(String smurl) {
		this.smurl = smurl;
	}

	public String getSmflag() {
		return smflag;
	}

	public void setSmflag(String smflag) {
		this.smflag = smflag;
	}

	public List<TSysmenu> getList() {
		return list;
	}

	public void setList(List<TSysmenu> list) {
		this.list = list;
	}

	public List<Map<String, String>> getTreeData() {
		return treeData;
	}

	public void setTreeData(List<Map<String, String>> treeData) {
		this.treeData = treeData;
	}

	@Override
	public String execute() throws Exception {
		System.out.println("execute");
		return SUCCESS;
	}
	
	/**
	 * 得到菜单树
	 * @return 树数据
	 */
	@SuppressWarnings("unchecked")
	public String getSysMenu(){
		list = systemMenuService.findAll();
		treeData = new ArrayList<Map<String,String>>();
		for(TSysmenu t:list){
			if(t.getTSmParentmenu()!=null){
				Map m = new HashMap();
				m.put("smid",t.getTSmId());
				m.put("id", t.getTSmCode());
				m.put("pid",t.getTSmParentmenu()==null?"":t.getTSmParentmenu());
				m.put("text", t.getTSmName());
				m.put("url", t.getTSmUrl());
				m.put("icon", t.getTSmIcon());
				m.put("order", t.getTSmOrder());
				m.put("useflg", t.getTSmUseflg());
				treeData.add(m);
			}
		}
		return "treeData";
	}
	
	/**
	 * 根据操作operation表示执行增加或修改树节点
	 */
	public void edit(){
		
		
		System.out.println("operation:"+operation);
		if("add".equals(operation)){
			TSysmenu sysmenu = new TSysmenu();
			sysmenu.setTSmCode(smcode);
			sysmenu.setTSmIcon(smimage);
			sysmenu.setTSmName(this.smname);
			sysmenu.setTSmOrder(Integer.parseInt(smorder));
			sysmenu.setTSmParentmenu(tsmId);
			sysmenu.setTSmUrl(smurl);
			sysmenu.setTSmUseflg(Integer.parseInt(smflag));
			systemMenuService.save(sysmenu);
		};
		if("modify".equals(operation)){
			TSysmenu sysmenu = systemMenuService.findById(tsmId);
			sysmenu.setTSmCode(smcode);
			sysmenu.setTSmIcon(smimage);
			sysmenu.setTSmName(smname);
			sysmenu.setTSmOrder(Integer.parseInt(smorder));
			sysmenu.setTSmUrl(smurl);
			sysmenu.setTSmUseflg(Integer.parseInt(smflag));
			systemMenuService.update(sysmenu);
		}
	}
	
	/**
	 * 删除树节点
	 */
	public void delete(){
		systemMenuService.delete(tsmId);
	}
}
